package com.appspot.modutajataja;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

public class GameScore extends Activity {

	private long result_time;
	private String gameName;
	private String userId;
	private Typeface font;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.game_score);
		
		font = Typeface.createFromAsset(getAssets(), "fonts/cartoonist.ttf");

		TextView best_score = (TextView) findViewById(R.id.best_score);
		best_score.setTypeface(font);
		TextView time_score = (TextView) findViewById(R.id.time_score);
		time_score.setTypeface(font);

		ImageView logoimage = (ImageView) findViewById(R.id.imageView1);

		// 인텐트받기
		Intent intent = getIntent();
		
		gameName = intent.getStringExtra("gameName");
		result_time = intent.getLongExtra("result_time", 0);
		userId = intent.getStringExtra("userId");
		time_score.setText(String.format("%02d : %02d : %02d", result_time/1000/60, (result_time/1000)%60, (result_time%1000)/10));
		
		if (intent.getStringExtra("gameName").equalsIgnoreCase("moaGame")) {
			logoimage.setImageResource(R.drawable.moa);
		} else if (intent.getStringExtra("gameName").equalsIgnoreCase("touchGame")) {
			logoimage.setImageResource(R.drawable.touch);
		} else if (intent.getStringExtra("gameName").equalsIgnoreCase("crashGame")) {
			logoimage.setImageResource(R.drawable.crash);
		}
		new addGameScore().execute();
	}
	
	class addGameScore extends AsyncTask<Void, Void, Void> {
		public addGameScore() { ; }

		@Override
		protected Void doInBackground(Void... params) {
			try {
				HttpClient client = new DefaultHttpClient();
				HttpPost post = new HttpPost(C.updateGameInfoURL);

				List<NameValuePair> scoreList = new ArrayList<NameValuePair>();
				scoreList.add(new BasicNameValuePair("userId", userId));
				scoreList.add(new BasicNameValuePair("gameName", gameName));
				scoreList.add(new BasicNameValuePair("gameTime", ""+result_time));
				
				post.setEntity(new UrlEncodedFormEntity(scoreList, HTTP.UTF_8));	
				HttpResponse response = client.execute(post);
			} catch (Exception e) {
				e.printStackTrace();
			}
			return null;
		}
	}

	public void ScoreOnClick(View v) {		
		startActivity(new Intent(GameScore.this, GameActivity.class).putExtra("userId", userId));
		GameScore.this.finish();
	}

	@Override
	public void onBackPressed() {

		AlertDialog.Builder alt_bld = new AlertDialog.Builder(this);
		alt_bld.setMessage("게임을 종료 하시겠습니까?")
				.setCancelable(false)
				.setPositiveButton("Yes",
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog, int id) {
								finish();
							}
						})
				.setNegativeButton("No", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int id) {
						dialog.cancel();
					}
				});
		AlertDialog alert = alt_bld.create();
		alert.setTitle("게임 종료");
		alert.setIcon(R.drawable.ic_launcher);
		alert.show();
	}
};